package com.kafka.interceptor;

import java.util.Map;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

/**
 * 自定义前缀
 * @author 苗建峰
 *
 */

public class TimeInterceptor implements ProducerInterceptor<String, String>{

	private String preffix;

	/**
	 * 定义拦截器的方法
	 */
	@Override
	public void configure(Map<String, ?> configs) {

		preffix = (String) configs.get("preffix");

	}

	/**
	 * 自定义Record
	 * @param record 原始Record
	 * @return 修改后的Record
	 */
	@Override
	public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {

		Long timestamp = System.currentTimeMillis();

		return new ProducerRecord<String, String>(
				record.topic(),
				record.partition(),
				record.timestamp(),
				record.key(),
				preffix + record.value(),
				record.headers()
		);

	}

	/**
	 * 收到ack以后调用
	 */
	@Override
	public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
		// TODO Auto-generated method stub

	}

	/**
	 * 关闭Producer时候调用
	 */
	@Override
	public void close() {
		// TODO Auto-generated method stub

	}



}
